﻿using Microsoft.AspNetCore.DataProtection;
using Microsoft.IdentityModel.Tokens;

namespace CoreWebApiNuGet.JwtAuthentication
{
    /// <summary>
    /// JWT票据内容
    /// </summary>
    public class JwtIssueOptions
    {
        /// <summary>
        /// 签发者
        /// </summary>
        public string Issuer { get; set; }

        /// <summary>
        /// 持票人
        /// </summary>
        public string Audience { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime Expires { get; set; }

        /// <summary>
        /// 签名秘钥
        /// </summary>
        public string Secret { get; set; }

        /// <summary>
        /// 默认加密方式
        /// </summary>
        public string DefaultSecurityType { get; set; }

        /// <summary>
        /// 签名验证
        /// </summary>
        public SigningCredentials SigningCredentials => BuildSigningCredentials(Secret, DefaultSecurityType);

        /// <summary>
        /// 构建签名凭证
        /// </summary>
        /// <param name="secret"></param>
        /// <param name="DefaultSecurityType"></param>
        /// <returns></returns>
        private SigningCredentials BuildSigningCredentials(string secret, string DefaultSecurityType)
        {
            switch (DefaultSecurityType)
            {
                case SecurityAlgorithms.HmacSha256:
                    return JwtUtility.BuildHmacSha256Credentials(secret);

                default:
                    return JwtUtility.BuildHmacSha256Credentials(secret);
            }
        }
    }
}
